package com.androidjam.runningacoruna;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class AddNewTrackActivity extends Activity {

	private ListView myList;
	private MyAdapter myAdapter;
	private static final int OK_RESULT_CODE = 1;
	private static final int CANCEL_RESULT_CODE = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_track_activity);

		myList = (ListView) findViewById(R.id.listViewAddTrack);
		myList.setItemsCanFocus(true);
		myAdapter = new MyAdapter();
		myList.setAdapter(myAdapter);
		
		Button addNewTrack = (Button) findViewById(R.id.buttonAddTrack);	
		
		addNewTrack.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				Intent intent = new Intent();
			    intent.putExtra("result", "Added");
			    setResult(OK_RESULT_CODE, intent);
				finish();

			}
		});
		
		
		myList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				Toast.makeText(view.getContext(), "CLICK",
						Toast.LENGTH_LONG).show();
			}
		});
		
	}
	
	public void onBackPressed() {
        Log.d("ADDNEWTRACKACTIVITY", "onBackPressed Called");
        Intent intent = new Intent();
	    intent.putExtra("result", "'Valor devuelto por ParametrosActivity'");
	    setResult(CANCEL_RESULT_CODE, intent);
		finish(); 

        return;
    }   
	
	
	public class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;
		public ArrayList<ListItem> myItems = new ArrayList<ListItem>();

		public MyAdapter() {
			mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
					
			ListItem listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_name_tag);
			myItems.add(listItem);
			
			listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_type_tag);
			myItems.add(listItem);
			
			listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_points_tag);
			myItems.add(listItem);
			
			listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_description_tag);
			myItems.add(listItem);
			
			listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_distance_tag);
			myItems.add(listItem);
			
			listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_elevation_tag);
			myItems.add(listItem);
			
			listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_calories_tag);
			myItems.add(listItem);
									
			listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_stimatedTime_tag);
			myItems.add(listItem);
			
			listItem = new ListItem();
			listItem.caption = getResources().getString(R.string.track_heart_rythm_tag);
			myItems.add(listItem);
			
			
			notifyDataSetChanged();
		}

		public int getCount() {
			return myItems.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.item, null);
				holder.caption = (EditText) convertView
						.findViewById(R.id.ItemCaption);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			//Fill EditText with the value you have in data source
			holder.caption.setText(myItems.get(position).caption);
			holder.caption.setId(position);

			//we need to update adapter once we finish with editing
			holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
				public void onFocusChange(View v, boolean hasFocus) {
					if (!hasFocus){
						final int position = v.getId();
						final EditText Caption = (EditText) v;
						myItems.get(position).caption = Caption.getText().toString();
					}
				}
			});

			return convertView;
		}
	}

	class ViewHolder {
		EditText caption;
	}

	class ListItem {
		String caption;
	}
	
/*
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	*/
}